#!/usr/bin/env python
"""

================================================================================

    abc2esac - ABC to EsAC conversion
    Copyright (C) 2010, Marcus Weseloh

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.


================================================================================
"""

import os.path
from optparse import OptionParser

from abcesac import __version__ as version, abc_to_esac

def main():
    parser = OptionParser(usage='usage: %prog [options] <ABC_FILE>',
                         version='%%prog %s' % version)
    parser.add_option('-s', '--segment', dest='segment',
                      action='store_true', default=False,
                      help='Segment songs into phrases using the meter and '
                      'grouper helper applications (default: False)')
    parser.add_option('-m', '--meter', dest='meter_cmd', 
                      help='path to meter executable',
                      metavar='PATH', default=None)
    parser.add_option('-g', '--grouper', dest='grouper_cmd',
                      help='path to grouper executable',
                      metavar='PATH', default=None)
    parser.add_option('-p', '--grouperparams', dest='grouper_params',
                      help='path to grouper parameter file',
                      metavar='PATH', default=None)
    parser.add_option('-o', '--output', dest='output',
                      help='write converted song(s) to FILE',
                      metavar='FILE')
    parser.add_option('-c', '--collection', dest='collection',
                      help='Collection name for converted EsAC songs',
                      metavar='NAME', default=None)
    parser.add_option('-k', '--key', dest='prefix',
                      help='Prefix for converted EsAC song keys',
                      metavar='PREFIX', default='')

    (options, args) = parser.parse_args()

    if len(args) != 1:
        parser.error('Please provide a path to an ABC file!')

    if not os.path.exists(args[0]):
        parser.error('File not found!')

    abc = open(args[0], 'r').read()

    result = abc_to_esac(abc, collection=options.collection,
                         prefix=options.prefix, group=options.segment,
                         meter_cmd=options.meter_cmd,
                         grouper_cmd=options.grouper_cmd,
                         grouper_params=options.grouper_params)

    if options.output:
        open(options.output, 'w').write(result)
    else:
        print result

if __name__ == '__main__':
    main()
